package com.dimples.dd.xxl.job.plus.prop;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;

/**
 * 配置
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/10/18
 */
@Configuration
@ConfigurationProperties(XxlJobPlusProperties.PREFIX)
public class XxlJobPlusProperties {

    public static final String PREFIX = "xxl.job.plus";

    @Setter
    @Getter
    private AdminProperties admin = new AdminProperties();

    @Setter
    @Getter
    private ExecutorProperties executor = new ExecutorProperties();

    @JsonIgnore
    @Autowired
    private Environment environment;

    @PostConstruct
    public void init() {
        if (!StringUtils.hasLength(this.getExecutor().getServiceName())) {
            String serviceName = environment.resolvePlaceholders("${xxl.job.plus.executor.serviceName:}");
            if (!StringUtils.hasLength(serviceName)) {
                serviceName = environment.resolvePlaceholders("${spring.application.name}");
            }
            this.getExecutor().setServiceName(serviceName);
        }
    }

    @Setter
    @Getter
    public static class AdminProperties {

        private String serviceName = "xxl-job-admin";

        private String contextPath = "/xxl-job-admin";

        private String accessToken = "default_token";

    }

    @Setter
    @Getter
    public static class ExecutorProperties {

        private String serviceName;

        private String logPath = "./logs/xxl-job/jobhandler";

        private Integer logRetentionDays = 30;

    }
}
